package 二分法查找数据;

import java.util.Scanner;

public class Demo {
	public static void main(String[] args) {
		int arr[] = { 0, 1, 3, 4, 5, 6, 9, 12, 61 };
		System.out.println("请输入要查找的元素：");
		Scanner s = new Scanner(System.in);
		int se = s.nextInt();
		System.out.println(search(arr, se));
	}

	public static int search(int[] arr, int search) {
		int min = 0;
		int max = arr.length - 1;
		int mid = (min + max) / 2;
		while (search != arr[mid]) {
			if (arr[mid] > search) {
				max = mid - 1;
			} else {
				min = mid + 1;
			}
			if (min > max) {
				return -1;
			}
			mid = (min + max) / 2;
		}
		return mid;
	}
}
